var moveBar = function (options) {
    this.options = options;
    this.html = $('<div id="movebar"></div>');

    this.setPosition(this.options.position);

    this.html
        .mousedown((e) => {
            this.html.css({
                'left': '0px',
                'right': '0px',
                'top': '0px',
                'bottom': '0px',
                'width': 'auto',
                'height': 'auto'
            });
            this.html.mousemove((e) => {
                var x = e.pageX || e.clientX + document.body.scroolLeft;
                var y = e.pageY || e.clientY + document.body.scroolLeft;
                var num = this.options.type == 'x' ? x : y;
                num = this.check(num);
                this.move(num);
            });
        }).mouseup((e) => {
            var x = e.pageX || e.clientX + document.body.scroolLeft;
            var y = e.pageY || e.clientY + document.body.scroolLeft;
            var num = this.options.type == 'x' ? x : y;
            num = this.check(num);
            this.setPosition(num);
            this.html.off('mousemove');
            this.move(num);
        });
    this.event = {
        move: [],
        check: []
    }
};
moveBar.prototype = {
    constructor: this,
    render: function () {
        return this.html;
    },
    setPosition: function (position) {
        if (this.options.type == 'x') {
            this.html.css({
                'left': (position - 2) + 'px',
                'right': 'auto',
                'width': '4px',
                'top': '0px',
                'bottom': '0px',
                'height': 'auto'
            });
        } else if (this.options.type == 'y') {
            this.html.css({
                'left': '0px',
                'right': '0px',
                'bottom': 'auto',
                'top': (position - 2) + 'px',
                'width': 'auto',
                'height': '4px'
            });
        };

    },
    on: function (str, event) {
        if (this.event.hasOwnProperty(str)) {
            this.event[str].push(event);
        }
    },
    check: function (num) {
        var newNum = 0;
        for (let i = 0; i < this.event.check.length; i++) {
            const element = this.event.check[i];
            newNum = element(num);
        };
        return newNum;
    },
    move: function (num) {
        for (let i = 0; i < this.event.move.length; i++) {
            const element = this.event.move[i];
            element(num);
        };
    }
};
module.exports = function (options) {
    return new moveBar(options);
};